/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.unindra.raport.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import edu.unindra.raport.dao.entity.Guru;
import edu.unindra.raport.dao.interf.GuruInterf;
import edu.unindra.raport.dao.koneksi.DatabaseUtilities;

/**
 * 
 * @author fendi
 */
public class GuruImpl implements GuruInterf {

	@Override
	public int insert(Guru gi) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"insert into guru values (?,?,?,?,?,?,?,?)");

			ps.setString(1, gi.getNip());
			ps.setString(2, gi.getNama());
			ps.setString(3, gi.getTeTgl());
			ps.setString(4, gi.getTaTgl());
			ps.setString(5, gi.getJbtn());
			ps.setString(6, gi.getPndkn());
			ps.setString(7, gi.getBidStudy());
			ps.setString(8, gi.getAlamat());
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}

	@Override
	public Guru getGuru() throws SQLException {
		Guru gi = new Guru();

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from guru");
			while (rs.next()) {
				gi.setNip(rs.getString("NIP"));
				gi.setNama(rs.getString("NAMA_GURU"));
				gi.setTeTgl(rs.getString("TEMPAT_LAHIR"));
				gi.setTaTgl(rs.getString("TGL_LAHIR"));
				gi.setJbtn(rs.getString("JABATAN"));
				gi.setPndkn(rs.getString("PENDIDIKAN"));
				gi.setBidStudy(rs.getString("BIDANG_STUDY"));
				gi.setAlamat(rs.getString("ALAMAT"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return gi;
	}

	@Override
	public List<Guru> getGuruAll() throws SQLException {
		List<Guru> sis = new LinkedList<Guru>();
		Guru gi = null;

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from guru");
			while (rs.next()) {
				gi = new Guru();

				gi.setNip(rs.getString("NIP"));
				gi.setNama(rs.getString("NAMA_GURU"));
				gi.setTeTgl(rs.getString("TEMPAT_LAHIR"));
				gi.setTaTgl(rs.getString("TGL_LAHIR"));
				gi.setJbtn(rs.getString("JABATAN"));
				gi.setPndkn(rs.getString("PENDIDIKAN"));
				gi.setBidStudy(rs.getString("BIDANG_STUDY"));
				gi.setAlamat(rs.getString("ALAMAT"));

				sis.add(gi);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return sis;
	}

	@Override
	public int delete(String id) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities.getInstance().getConnection()
					.prepareStatement("delete from guru where NIP = ?");

			ps.setString(1, id);
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public int update(Guru si) throws SQLException {
		int result = -1;

		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"update guru set NAMA_GURU = ?,TEMPAT_LAHIR = ?,TGL_LAHIR = ?,JABATAN = ?,PENDIDIKAN = ?,BIDANG_STUDY = ?,ALAMAT = ? where NIP = ? ");

			ps.setString(1, si.getNama());
			ps.setString(2, si.getTeTgl());
			ps.setString(3, si.getTaTgl());
			ps.setString(4, si.getJbtn());
			ps.setString(5, si.getPndkn());
			ps.setString(6, si.getBidStudy());
			ps.setString(7, si.getAlamat());
			ps.setString(8, si.getNip());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}
}
